package com.love.marriage.userCallRetrieval.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userCallRetrieval.entity.UserCallRetrieval;
import com.love.marriage.userCallRetrieval.mapper.UserCallRetrievalMapper;
import com.love.marriage.userCallRetrieval.service.UserCallRetrievalService;
import com.love.marriage.userCallRetrieval.vo.UserCallRetrievalVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户获取电话次数记录表服务实现类
 * author:       mybatisPlus
 * createDate:   2025-01-17 19:28:01
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserCallRetrievalServiceImp extends ServiceImpl<UserCallRetrievalMapper, UserCallRetrieval> implements UserCallRetrievalService {

    private final UserCallRetrievalMapper userCallRetrievalMapper;

    @Override
    public Page<UserCallRetrievalVo> getPage(Long pageNum, Long pageSize, UserCallRetrievalVo userCallRetrievalVo) {
        Page<UserCallRetrievalVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userCallRetrievalMapper.getPage(page, userCallRetrievalVo);
    }

    @Override
    public UserCallRetrievalVo queryUserCallRetrieval(Long id) {
        return userCallRetrievalMapper.queryUserCallRetrieval(id);
    }

    @Override
    public UserCallRetrievalVo queryUserCallRetrievalByUerId(Long userId) {
        return userCallRetrievalMapper.queryUserCallRetrievalByUerId(userId);
    }

    @Override
    public UserCallRetrievalVo addUserCallRetrieval(UserCallRetrievalVo userCallRetrievalVo) {
        UserCallRetrieval userCallRetrieval = new UserCallRetrieval();
        BeanUtils.copyProperties(userCallRetrievalVo, userCallRetrieval);
        userCallRetrievalMapper.insert(userCallRetrieval);
        UserCallRetrievalVo res = new UserCallRetrievalVo();
        BeanUtils.copyProperties(userCallRetrieval, res);
        return res;
    }

    @Override
    public UserCallRetrievalVo updateUserCallRetrieval(UserCallRetrievalVo userCallRetrievalVo) {
        UserCallRetrieval userCallRetrieval = new UserCallRetrieval();
        BeanUtils.copyProperties(userCallRetrievalVo, userCallRetrieval);
        userCallRetrievalMapper.updateById(userCallRetrieval);
        return userCallRetrievalVo;
    }

    @Override
    public Boolean deleteUserCallRetrieval(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userCallRetrievalMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    public Boolean addUserCallRetrievalCount(Long userId) {
        UserCallRetrievalVo userCallRetrievalVo = queryUserCallRetrievalByUerId(userId);
        if (userCallRetrievalVo == null) {
            userCallRetrievalVo = new UserCallRetrievalVo();
            userCallRetrievalVo.setUserId(userId);
            userCallRetrievalVo.setRetrievalCount(1);
            userCallRetrievalVo.setRetrievalTime(LocalDateTime.now());
            addUserCallRetrieval(userCallRetrievalVo);
        } else {
            userCallRetrievalVo.setRetrievalCount(userCallRetrievalVo.getRetrievalCount() + 1);
            userCallRetrievalVo.setRetrievalTime(LocalDateTime.now());
            updateUserCallRetrieval(userCallRetrievalVo);
        }
        return true;
    }
}
